package com.ruoyi.gpt.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.core.config.utils.WxGptUtil;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gpt.domain.vo.GptUserVo;
import com.ruoyi.gpt.domain.bo.GptUserBo;
import com.ruoyi.gpt.service.IGptUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * chatGpt用户
 *
 * @author ztx
 * @date 2023-03-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gpt/chatGpt")
public class GptUserController extends BaseController {

    private final IGptUserService iGptUserService;

    /**
     * 查询chatGpt用户列表
     */
    @SaCheckPermission("gpt:chatGpt:list")
    @GetMapping("/list")
    public TableDataInfo<GptUserVo> list(GptUserBo bo, PageQuery pageQuery) {
        return iGptUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出chatGpt用户列表
     */
    @SaCheckPermission("gpt:chatGpt:export")
    @Log(title = "chatGpt用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GptUserBo bo, HttpServletResponse response) {
        List<GptUserVo> list = iGptUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "chatGpt用户", GptUserVo.class, response);
    }

    /**
     * 获取chatGpt用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gpt:chatGpt:query")
    @GetMapping("/{id}")
    public R<GptUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iGptUserService.queryById(id));
    }

    /**
     * 新增chatGpt用户
     */
    @SaCheckPermission("gpt:chatGpt:add")
    @Log(title = "chatGpt用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GptUserBo bo) {
        return toAjax(iGptUserService.insertByBo(bo));
    }

    /**
     * 修改chatGpt用户
     */
    @SaCheckPermission("gpt:chatGpt:edit")
    @Log(title = "chatGpt用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GptUserBo bo) {
        return toAjax(iGptUserService.updateByBo(bo));
    }

    /**
     * 删除chatGpt用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gpt:chatGpt:remove")
    @Log(title = "chatGpt用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iGptUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
    /**
     * 获取用户详情
     */
    @GetMapping("/getUser/info")
    public R<GptUserVo> getUserInfo(){
        return R.ok(WxGptUtil.getUserVoByToken());
    }
}
